<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/18
 * Time: 10:39
 */

namespace App\Logic;


use App\Model\AppModel;
use App\Model\BlacklistModel;
use App\Model\UserModel;
use App\Services\HelpService;
use App\Services\ReponseCodeService;
use EasySwoole\ORM\DbManager;

class UserLogic
{
    public function getOfflineTime($userId){
        $redisPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redis');
        $redis = $redisPool->getObj();
        $offlineTime = $redis->hGet(Config::getInstance()->getConf('CHAT.user_offline_time'),$userId);
        $redisPool->recycleObj($redis);
    }
    /**用户是否存在
     * @param $where
     */
    public function userExit($where){
        try{
           if(UserModel::create()->where($where)->column('id')){
               return true;
            }
            return false;
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**
     * 通过三方唯一标志查询
     */
    public function findByAppUserId($accessKey,$AppUserId){
        try{
            $app = AppModel::create()->get(['access_key'=>$accessKey]);
            if(empty( $app)){
                return false;
            }

            $user = UserModel::create()->get(['app_user_id'=>$AppUserId,'app_id'=>$app['id']]);
           return $user;
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**
     * 通过三方唯一标志查询
     */
    public function findByAppUserIdByAppId($appId,$appUserId){
        try{
            $user = UserModel::create()->get(['app_user_id'=>$appUserId,'app_id'=>$appId]);
            if(empty($user)){
                $userModel = new UserModel();
                $userData['app_user_id'] =$appUserId;
                $userData['app_id'] = $appId;
                $userModel->createUser($userData);
                $user = UserModel::create()->get(['app_user_id'=>$appUserId,'app_id'=>$appId]);
            }
            return $user;
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**修改用户信息
     * @param $userId
     * @param $updateInfo
     */
    function updateInfo($userId,$updateInfo){
        try{
            $user = UserModel::create()->get($userId);
            return $user->update($updateInfo);
        }catch(\Throwable $t){
            return ReponseCodeService:: CODE_SERVICE_UNAVAILABLE;
        }
    }

    /**
     * @param $userId
     * @param $dislikeUserId
     */
    function addBlackList($userId,$dislikeUserId){
         try{
                //开启事务
                DbManager::getInstance()->startTransaction();
                #判断是否已经是黑名单
                 $dislikeData['user_id'] = $userId;
                 $dislikeData['dislike_user_id'] = $dislikeUserId;
                 $dislikeData['created_at'] = HelpService::msectimeToStr(HelpService::msectime());
                 $dislikeData['updated_at'] = HelpService::msectimeToStr(HelpService::msectime());

                 if(BlacklistModel::create()->get(['user_id'=>$userId,'dislike_user_id'=>$dislikeUserId])){
                     return false;
                 }
             BlacklistModel::create()->data($dislikeData)->save();
                #创建黑名单
            } catch(\Throwable  $e){
                //回滚事务
                DbManager::getInstance()->rollback();
                return false;
            } finally {
                //提交事务
                DbManager::getInstance()->commit();
            }
            return true;
    }

    /**
     * @param $userId
     * @param $dislikeUserId
     */
    function delBlackList($userId,$dislikeUserId){
        try{
            //开启事务
            DbManager::getInstance()->startTransaction();
            #判断是否已经是黑名单
            if(!BlacklistModel::create()->get(['user_id'=>$userId,'dislike_user_id'=>$dislikeUserId])){
                return false;
            }
            BlacklistModel::create()->destroy(['user_id'=>$userId,'dislike_user_id'=>$dislikeUserId]);
            #创建黑名单
        } catch(\Throwable  $e){
            //回滚事务
            DbManager::getInstance()->rollback();
            return false;
        } finally {
            //提交事务
            DbManager::getInstance()->commit();
        }
        return true;
    }
}